package com.home766.template.directive;

import com.home766.entity.Customer;
import com.home766.service.ICustomerService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.biz.template.directive.BaseDirective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("customerListDirective")
public class CustomerListDirective extends BaseDirective {

	@Autowired
	private ICustomerService customerService;
	
	/** 变量名称 */
	private static final String VARIABLE_NAME = "customers";
	
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		List<Customer> customers;
		boolean useCache = useCache(env, params);
		customers = customerService.findAll();
		List<Map<String, Object>> results=new ArrayList<Map<String,Object>>();
		Map<String, Object> result=new HashMap<String, Object>();
		if(customers!=null&&customers.size()>0){
			Customer c=customers.get(0);
			result.put("tel", c.getTel());
			result.put("workTime", c.getWorkTime());
			result.put("flag", c.getFlag());
			Map<String, Object> map1=null;
			Map<String, Object> map2=null;
			Map<String, Object> map3=null;
			Map<String, Object> map4=null;
			if(null!=c.getTencentId1()&&!"".equals(c.getTencentId1().trim())){
				if(null==map1){
					map1=new HashMap<String, Object>();
				}
				map1.put("qq", c.getTencentId1());
			}
			if(null!=c.getTencentId2()&&!"".equals(c.getTencentId2().trim())){
				if(null==map2){
					map2=new HashMap<String, Object>();
				}
				map2.put("qq", c.getTencentId2());
			}
			if(null!=c.getTencentId3()&&!"".equals(c.getTencentId3().trim())){
				if(null==map3){
					map3=new HashMap<String, Object>();
				}
				map3.put("qq", c.getTencentId3());
			}
			if(null!=c.getTencentId4()&&!"".equals(c.getTencentId4().trim())){
				if(null==map4){
					map4=new HashMap<String, Object>();
				}
				map4.put("qq", c.getTencentId4());
			}
			
			if(null!=c.getName1()&&!"".equals(c.getName1().trim())){
				if(null==map1){
					map1=new HashMap<String, Object>();
				}
				map1.put("name", c.getName1());
			}
			if(null!=c.getName2()&&!"".equals(c.getName2().trim())){
				if(null==map2){
					map2=new HashMap<String, Object>();
				}
				map2.put("name", c.getName2());
			}
			if(null!=c.getName3()&&!"".equals(c.getName3().trim())){
				if(null==map3){
					map3=new HashMap<String, Object>();
				}
				map3.put("name", c.getName3());
			}
			if(null!=c.getName4()&&!"".equals(c.getName4().trim())){
				if(null==map4){
					map4=new HashMap<String, Object>();
				}
				map4.put("name", c.getName4());
			}
			if(null!=map1){
				results.add(map1);
			}
			if(null!=map2){
				results.add(map2);
			}
			if(null!=map3){
				results.add(map3);
			}
			if(null!=map4){
				results.add(map4);
			}
		}
		result.put("list", results);
		setLocalVariable(VARIABLE_NAME, result, env, body);
	}
}
